/*
 * Copyright@www.it1997.com.
 * @Author: 陈晓晨
 * @Date: 2020/7/16
 * Description:版权所有 QQ:376231445
 */

package com.it1997.it1997admin;



import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import lombok.extern.log4j.Log4j2;

import java.io.*;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

@Log4j2
public  class  JschUtils {
    //建立连接
    public static Session createConnection(String host,String userName, String password, int port) {
        JSch jsch = new JSch();
        Session session = null;
        try {
            session = jsch.getSession(userName, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setTimeout(6000);
            session.connect();
            return session;
        } catch (Exception e) {
            log.error(e.getMessage());
            return session;
        }

    }

    //发送命令
    public static InputStream sendCommand(Session session, String command) {
        InputStream in = null;
        try {
            ChannelExec exec = (ChannelExec) session.openChannel("exec");
            in = exec.getInputStream();
            exec.setCommand(command);
            exec.connect();
            return in;
        } catch (Exception e) {
            log.info(e.getMessage());
        }
        return in;
    }
    public static String returnData(InputStream in) throws IOException {
        StringBuffer buffer = new StringBuffer();;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
            String line=null;
            int i=0;
            while((line=br.readLine()) != null){
                if(i==1){
                    buffer.append(line+"\n");
                }else if(i==3){
                    buffer.append(line+"\n");
                }
                i++;
            }
        } catch (UnsupportedEncodingException e) {
            log.error("解析脚本出错："+e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            log.error("解析脚本出错："+e.getMessage());
            e.printStackTrace();
        }
        in.close();
        return buffer.toString();
    }
    public static Map splitMemResult(String str){
        Map map=new HashMap();
        DecimalFormat df   = new DecimalFormat("######0.00");
        double memUsed=0.0;
        double memTotal=0.0;
                String [] arr = str.split("\\s+");
        for(int i=1;i<4;i++) {
            if (i == 1) {
                System.out.println("总内存为：" + arr[i]);
                memTotal= Double.parseDouble(arr[i]);
                map.put("memTotal",arr[i]);
            } else if (i == 2) {
                System.out.println("已使用：" + arr[i]);
                memUsed= Double.parseDouble(arr[i]);
                map.put("memUsed",arr[i]);
            } else if (i == 3) {
                System.out.println("剩余内存：" + arr[i]);
                map.put("memFree",arr[i]);
            }
        }
        System.out.println("内存使用率："+df.format((memUsed/memTotal)*100));
        map.put("ramUsage",df.format((memUsed/memTotal)*100));
        return map;
    }

    public static Map splitDiskResult(String str){
        Map map=new HashMap();
        String [] arr = str.split("\\s+");
        for(int i=0;i<5;i++) {
            if (i == 4) {
                System.out.println("磁盘使用率：" +arr[i].substring(0,arr[i].length()-1));
                map.put("diskUsage",arr[i].substring(0,arr[i].length()-1));
            }
        }
        return map;
    }

    public static Map splitCpuResult(String str){

        Map map=new HashMap();
        String [] arr = str.split("\\s+");
        for(int i=0;i<10;i++) {
            if (i == 8) {
                System.out.println("CPU空闲率：" + arr[i]);
                map.put("cpuFree",arr[i]);
            }

        }
        return map;
    }
}
